<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.needHerb" clearable class="filter-item" style="width: 160px;" placeholder="采购需求" />
    <el-date-picker
      class="filter-item"
      v-model="time"
      type="daterange"
      align="right"
      unlink-panels
      value-format="yyyy-MM-dd HH:mm:ss"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      :default-time="['00:00:00', '23:59:59']"
      :picker-options="queryPickerOptions">
    </el-date-picker>
      <el-select v-model="listQuery.status" clearable style="width: 200px" class="filter-item" placeholder="状态">
        <el-option v-for="(item, index) in statusDic" :key="item" :label="item" :value="index" />
      </el-select>
      <el-button v-permission="['GET /admin/purchase/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button v-permission="['POST /admin/purchase/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="采购计划" prop="name" />
      <el-table-column align="center" label="发布版本" prop="parent_id">
      <template slot-scope="scope">
          {{ scope.row.parentId==null?'第一次发布':'第二次发布' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="采购需求" prop="needHerb">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.needHerb" placement="top-start">
            <span>{{ellipsis(scope.row.needHerb)}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" label="截止时间" prop="dueTime" />
      <el-table-column align="center" label="联系人" prop="contactName" />
      <el-table-column align="center" label="电话" prop="contactTel" />
      <el-table-column align="center" label="发布时间" prop="addTime" />

      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          {{ statusDic[scope.row.status] }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status!='0'&&scope.row.status!='3'" type="primary" size="mini" @click="handleView(scope.row)">查看</el-button>
          <el-button v-if="scope.row.status=='3'" v-permission="['POST /admin/purchase/update']" type="warning" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-if="scope.row.status=='0'" v-permission="['POST /admin/purchase/update']" type="success" size="mini" @click="handleUpdate(scope.row)">发布</el-button>
          <el-button v-if="scope.row.status!='5'" v-permission="['POST /admin/purchase/delete']" type="danger" size="mini" @click="handleDelete(scope.row)">中止</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="150px" style="width: 800px; margin-left:50px;">
        <el-form-item label="采购计划名" prop="name" style="width: 400px;">
          <el-input v-if="dialogStatus!='view'" v-model="dataForm.name" />
          <span v-else class="form-span">{{dataForm.name}}</span>
        </el-form-item>
        <el-form-item label="报价截止时间" prop="dueTime" style="width: 400px;">
          <el-date-picker
            v-if="dialogStatus!='view'"
            style="width: 250px;"
            v-model="dataForm.dueTime"
            type="datetime"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="23:59:59"
            :picker-options="pickerOptions"
          />
          <span v-else class="form-span">{{dataForm.dueTime}}</span>
        </el-form-item>
        <el-form-item label="收货地址" prop="addressId" style="width: 800px;">
          <el-select v-if="dialogStatus!='view'" @change="selectChange" v-model="dataForm.addressId" clearable style="float:left;width: 250px" class="filter-item" placeholder="收货地址">
            <el-option v-for="(item) in addressList" :key="item.id" :label="item.addressDetail" :value="item.id">
              <span style="float: left color: #8492a6; font-size: 13px">{{ item.province+item.city+item.county  }}</span>
              <span style="float: right; ">{{ item.addressDetail }}</span>
            </el-option>
          </el-select>
          <span v-else class="form-span">{{dataForm.inventoryPlace}}</span>
          <el-button v-if="dialogStatus!='view'" type="primary" size="mini" @click="addAddress()" style="margin-left:10px;float:left;">添加</el-button>
        </el-form-item>
        <el-form-item label="联系人" prop="contactName" style="width: 400px;">
          <el-input v-if="dialogStatus!='view'" v-model="dataForm.contactName" />
          <span v-else class="form-span">{{dataForm.contactName}}</span>
        </el-form-item>
        <el-form-item label="联系人电话" prop="contactTel" style="width: 400px;">
          <el-input v-if="dialogStatus!='view'" v-model="dataForm.contactTel" />
          <span v-else class="form-span">{{dataForm.contactTel}}</span>
        </el-form-item>
        <el-form-item v-if="dataForm.approveTime" label="审核时间" prop="approveTime" style="width: 400px;">
          <span class="form-span">{{dataForm.approveTime}}</span>
        </el-form-item>
        <el-form-item v-if="dataForm.approveRemark" label="审核说明" prop="approveRemark" style="width: 400px;">
          <span class="form-span">{{dataForm.approveRemark}}</span>
        </el-form-item>
        <el-button v-if="dialogStatus!='view'" :loading="downloadLoading" style="float:left;margin-bottom: 5px;" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload()">导入模板下载</el-button>
        <el-upload v-if="dialogStatus!='view'" class="filter-item" action=""  :http-request="uploadSectionFile"  :show-file-list="false" style="margin-left:10px;float:left;">
          <el-button class="in-item" type="primary" icon="el-icon-upload">
            导入
          </el-button>
        </el-upload>


        <!-- 查询结果 -->
    <el-table v-loading="dialogListLoading" height="250" :data="herbList" element-loading-text="正在获取药材信息。。。" border fit highlight-current-row>

      <el-table-column align="center" label="编号" prop="code" />

      <el-table-column align="center" label="药材名" prop="herbName" />
      
      <el-table-column align="center" label="规格" prop="specifications" />
      
      <el-table-column align="center" label="数量" prop="needAmount" />
      
      <el-table-column align="center" label="单位" prop="measurementUnit" />

      <el-table-column v-if="dialogStatus!='view'" align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="handleHerbListDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData(0)">保存</el-button>
        <el-button v-else-if="dialogStatus=='update'" type="primary" @click="updateData(0)">修改</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData(1)">保存并发布</el-button>
        <el-button v-else-if="dialogStatus=='update'" type="primary" @click="updateData(1)">修改并发布</el-button>
      </div>
    </el-dialog>

    <!-- 地址添加对话框 -->
    <el-dialog class="addressDialog" title="新地址" :visible.sync="addressVisible" >
      <el-form ref="addressDataForm" :rules="addressRules" :model="addressDataForm" status-icon label-position="left" label-width="110px" style="width: 580px; margin-left:50px;">
         <el-form-item label="联系人" prop="name">
          <el-input v-model="addressDataForm.name" maxlength="50" />
        </el-form-item>
          <el-form-item label="联系人手机" prop="tel">
          <el-input v-model="addressDataForm.tel" maxlength="50" />
        </el-form-item>

        <el-form-item label="区域地址" prop="city">
            <el-select @change="queryCity(addressDataForm.province)" v-model="addressDataForm.province" clearable placeholder="请选择" style="width: 150px;float:left;">
              <el-option
                v-for="item in province"
                :key="item.id"
                :label="item.name"
                :value="item.name">
              </el-option>
            </el-select>

            <el-select v-if="addressDataForm.province" @change="queryCounty(addressDataForm.city)" v-model="addressDataForm.city" clearable placeholder="请选择" style="width: 150px;margin-left:10px;float:left;">
              <el-option
                v-for="item in city"
                :key="item.id"
                :label="item.name"
                :value="item.name">
              </el-option>
            </el-select>
          
            <el-select v-if="addressDataForm.city" v-model="addressDataForm.county" clearable placeholder="请选择" style="width: 150px;margin-left:10px;float:left;">
              <el-option
                v-for="item in county"
                :key="item.id"
                :label="item.name"
                :value="item.name">
              </el-option>
            </el-select>
        </el-form-item>

     <el-form-item label="详细地址" prop="addressDetail">
          <el-input v-model="addressDataForm.addressDetail" maxlength="50" />
        </el-form-item>
   
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取消</el-button>
        <el-button type="primary" @click="createAddressData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 145px;
  height: 145px;
  display: block;
}
.addressDialog .el-dialog{
  width: 720px;
}
</style>

<script>
import { listPurchase,queryAddress,createPurchase, queryHerb,queryPurchaseItem, updatePurchase, deletePurchase } from '@/api/purchase'
import { listRegion ,addAddress} from '@/api/user'
import { uploadPath } from '@/api/storage'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { thumbnail, toPreview } from '@/utils/index'

export default {
  name: 'Purchase',
  components: { Pagination },
  data() {
    return {
      thumbnail,
      toPreview,
      uploadPath,
      list: [],
      total: 0,
      herbList:[],
      province:[],
      city:[],
      county:[],
      addressList:[],
      listLoading: true,
      dialogListLoading:false,
      downloadLoading: false,
      time:[],
      listQuery: {
        page: 1,
        limit: 20,
        remark:'',
        id: undefined,
        needHerb:'',
        startTime:'',
        endTime:'',
        name: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      dataForm: {
        id: undefined,
        name: '',
        floorPrice: undefined,
        picUrl: undefined
      },
      addressDataForm: {
        name: undefined,
        tel:undefined,
        province: undefined,
        county: undefined,
        city: undefined,
        addressDetail:undefined,
      },
      dialogFormVisible: false,
      addressVisible:false,
      dialogStatus: '',
      textMap: {
        update: '修改采购计划',
        create: '新采购计划'
      },
      statusDic: ['待发布', '审核中', '已发布','被驳回','已完结','已中止'],
      rules: {
        name: [{ required: true, message: '采购计划名不能为空', trigger: 'blur' }],
        dueTime:[{ required: true, message: '报价截止时间不能为空', trigger: 'blur' }],
        addressId:[{ required: true, message: '收货地址不能为空', trigger: 'blur' }],
      },
      addressRules: {
        name: [{ required: true, message: '联系人不能为空', trigger: 'blur' },{pattern:/^([a-zA-Z\u4e00-\u9fa5\·]{1,10})$/,message:'联系人姓名格式错误,请输入英文或汉字'}],
        tel: [{ required: true, message: '联系人手机不能为空', trigger: 'blur' },{pattern:/^1[3|4|5|7|8|9][0-9]\d{8}$/, message: '请输入有效的手机号码'}],
        city: [{ required: true, message: '区域地址不能为空', trigger: 'blur' }],
        addressDetail: [{ required: true, message: '详细地址不能为空', trigger: 'blur' }],
      },
      queryPickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      pickerOptions: {
        disabledDate(v) {
          return v.getTime() < new Date().getTime() - 86400000;
        }
      },
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    // 绑定enter事件
    this.enterKeyup();
  },
  destroyed() {
    // 销毁enter事件
    this.enterKeyupDestroyed();
  },
  methods: {
    enterKey(event) {
      if(event.key=='Enter'){
        this.handleFilter()
      }
    },
    enterKeyupDestroyed() {
        document.removeEventListener("keyup", this.enterKey);
    },
    enterKeyup() {
        document.addEventListener("keyup", this.enterKey);
    },
    getList() {
      this.listLoading = true
      if(this.time!=null){
        this.listQuery.startTime=this.time[0]
        this.listQuery.endTime=this.time[1]
      }else{
        this.listQuery.startTime=undefined
        this.listQuery.endTime=undefined
      }
      listPurchase(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.herbList = []
      this.dataForm = {
        id: undefined,
        name: undefined,
        floorPrice: undefined,
        picUrl: undefined,
        contactName: undefined,
        contactTel: undefined
      }
    },
    selectChange(e){
      if(e){
      var sub=this.addressList.findIndex((arr)=>arr.id==e)
      this.dataForm.contactName=this.addressList[sub].name
      this.dataForm.contactTel=this.addressList[sub].tel
      }
    },
    ellipsis (value) {
      if (!value) return ''
      if (value.length > 10) {
        return value.slice(0,10) + '...'
      }
      return value
    },
    handleCreate() {
      this.resetForm()
      queryAddress()
        .then(response => {
          this.dialogStatus = 'create'
          this.dialogFormVisible = true
          this.addressList= response.data.data
        })
        .catch(response => {
          this.dialogFormVisible = false
          this.$notify.error({
            title: '无法发布采购',
            message: response.data.errmsg
          })
        })

      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    resetAddressForm() {
      this.addressDataForm = {
         name: undefined,
        tel:undefined,
        province: undefined,
        county: undefined,
        city: undefined,
        addressDetail:undefined,
      }
    },
    addAddress(){
      this.addressVisible = true
      this.resetAddressForm()
      this.queryProvince(0)
      this.$nextTick(() => {
        this.$refs['addressDataForm'].clearValidate()
      })
    },
    queryProvince(parentId){
      let data = {}
      data.parentId=parentId
      listRegion(data).then(response => {
        this.province=response.data.data.list
      })
    },
    queryCity(province){
      let data = {}
      this.dataForm.city = ''
      this.province.forEach(element => {
        if(element.name==province){
        data.parentId=element.id
        listRegion(data).then(response => {
        this.city=response.data.data.list
      })
      }
      });
    },
    queryCounty(city){
      let data = {}
      this.dataForm.county = ''
      this.city.forEach(element => {
        if(element.name==city){
          data.parentId=element.id
          listRegion(data).then(response => {
          this.county=response.data.data.list
          })
        }
      });
    },
    createData(status) {
      var that = this
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.dataForm.status=status
          let data={}
          data.dataForm=this.dataForm
          data.herbList=this.herbList
          data.dataForm.inventoryplace = (that.addressList).find((arr)=>arr.id==data.dataForm.addressId).addressDetail
          if(data.herbList.length>5000){
            this.$message.error('采购药材最多只能有5000条，当前'+data.herbList.length+'条');
          }else 
          if(data.herbList.length!=0){
          this.$confirm('此操作将创建新采购, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          }).then(() => {
          createPurchase(data)
            .then(response => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '创建成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
            })
            }).catch(() => {
            this.stepLoading = false
            this.$message({
              type: 'info',
              message: '已取消创建'
            });          
          });
            }
            else{
              this.$message.error('采购药材不能为空');
            }
        }
      })
    },
    createAddressData() {
      this.$refs['addressDataForm'].validate(valid => {
        if (valid) {
          this.$confirm('此操作将添加地址, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          }).then(() => {
          addAddress(this.addressDataForm)
            .then(response => {
              this.addressVisible = false
              this.$notify.success({
                title: '添加成功'
              })
              this.resetAddressForm()
              queryAddress()
                .then(response => {
                  this.addressList= response.data.data
                })
                .catch(response => {
                  this.$notify.error({
                    title: '地址查询失败',
                    message: response.data.errmsg
                  })
                })
            })
            .catch(response => {
              this.$notify.error({
                title: '添加失败',
                message: response.data.errmsg
              })
            })
            }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消添加'
          });          
        });
        }
      })
    },
    handleView(row){
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'view'
      this.dialogFormVisible = true
      this.dialogListLoading = true
      queryAddress()
        .then(response => {
          this.addressList= response.data.data
        })
        let data={
          purchaseId:this.dataForm.id
        }
      queryPurchaseItem(data).then(response => {
          this.herbList= response.data.data
          this.dialogListLoading = false
        })
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.dialogListLoading = true
      queryAddress()
        .then(response => {
          this.addressList= response.data.data
        })
        let data={
          purchaseId:this.dataForm.id
        }
      queryPurchaseItem(data).then(response => {
          this.herbList= response.data.data
          this.dialogListLoading = false
        })

      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData(status) {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.dataForm.status=status
          let data={}
          data.dataForm=this.dataForm
          data.herbList=this.herbList
          data.dataForm.inventoryplace = (this.addressList).find((arr)=>arr.id==data.dataForm.addressId).addressDetail
          if(data.herbList.length!=0){
          this.$confirm('此操作将更新采购信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          }).then(() => {
          updatePurchase(data)
            .then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.errmsg
              })
            })
            }).catch(() => {
            this.stepLoading = false
            this.$message({
              type: 'info',
              message: '已取消更新'
            });          
          });
            }
            else{
              this.$message.error('采购药材不能为空');
            }
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将中止该采购, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deletePurchase(row)
        .then(response => {
          this.$notify.success({
            title: '成功',
            message: '中止成功'
          })
          this.getList()
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
        }).catch(() => {
                  
        })
      
    },
    handleHerbListDelete(row){
      this.herbList.splice((this.herbList).findIndex((arr)=>arr.code==row.code), 1)
    },
    // 导出的方法
    handleDownload() {
      this.downloadLoading = true
      this.listQuery.selectAll = true

          const { export_json_to_excel } = require('../../vendor/Export2Excel')// 注意这个Export2Excel路径
          const tHeader = ['药材编号', '总量(纯数字)'] // 上面设置Excel的表格第一行的标题
          const data = []
          export_json_to_excel(tHeader, data,'采购药材导入模板',true)// 最后一个是表名字

      this.downloadLoading= false
    },
    // 导入
    uploadSectionFile(file) { // file导入文件
    if(file.file){
      this.importfxx(file.file)
    }
    },
    importfxx(file) {
                let _this = this;
                let inputDOM = this.$refs.inputer;
                // 通过DOM取文件数据
                // this.file = event.currentTarget.files[0];
                this.file = file
                var rABS = false; //是否将文件读取为二进制字符串
                var f = this.file;
                var reader = new FileReader();
                //if (!FileReader.prototype.readAsBinaryString) {
                FileReader.prototype.readAsBinaryString = function(f) {
                    var binary = "";
                    var rABS = false; //是否将文件读取为二进制字符串
                    var pt = this;
                    var wb; //读取完成的数据
                    var outdata;
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        var bytes = new Uint8Array(reader.result);
                        var length = bytes.byteLength;
                        for(var i = 0; i < length; i++) {
                            binary += String.fromCharCode(bytes[i]);
                        }
                        var XLSX = require('xlsx');
                        if(rABS) {
                            wb = XLSX.read(btoa(fixdata(binary)), { //手动转化
                                type: 'base64'
                            });
                        } else {
                            wb = XLSX.read(binary, {
                                type: 'binary'
                            });
                        }
                        outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);//outdata就是你想要的东西
                        this.da = [...outdata]
                        let arr = []
                        let codeList = []
                        var i=2
                        var state=true
                        var numReg = /^[0-9]*$/
                        var numRe = new RegExp(numReg)
                        console.log(this.da.length)
                        if(this.da.length>5000){
                          state=false
                          _this.$notify.error({
                              title: '导入失败',
                              message: '采购药材最多只能有5000条，当前'+this.da.length+'条'
                            })
                        }
                        this.da.map(v => {
                          if(state){
                            let obj = {}
                            if(arr.findIndex((a) => a.code==v["药材编号"])!=-1){
                              _this.$notify.error({
                              title: '导入失败',
                              message: '第'+i+'行药材编号重复'
                            })
                              state=false
                            }
                            
                              codeList.push(v["药材编号"])
                              obj.code = v["药材编号"];      
                              obj.total = v["总量(纯数字)"];
                              if(!numRe.test(obj.total)){
                              _this.$notify.error({
                              title: '导入失败',
                              message: '第'+i+'行总量不为纯数字'
                            })
                              state=false
                            }
                              arr.push(obj)
                              i++
                            
                            }
                        })
                        if(state){
                          _this.dialogListLoading = true
                        queryHerb(codeList)
                          .then(response => {
                            response.data.data.forEach(element => {
                              element.needAmount = (arr).find((arr)=>arr.code==element.code).total/element.coefficient
                            })
                            _this.$notify.success({
                              title: '导入成功'
                            })
                            $("#upload").val('');
                            _this.dialogListLoading = false
                            _this.herbList= response.data.data
                          })
                          .catch(response => {
                            _this.dialogListLoading = false
                            _this.$notify.error({
                              title: '导入失败',
                              message: response.data.errmsg
                            })
                            $("#upload").val('');
                          })
                        }else{
                            $("#upload").val('');
                        }
                        
                        _this.$message({
                            message: '请耐心等待导入成功',
                            type: 'success'
                        });
                    }
                    reader.readAsArrayBuffer(f);
                }
                if(rABS) {
                    reader.readAsArrayBuffer(f);
                } else {
                    reader.readAsBinaryString(f);
                }
            },
  }
}
</script>
